<template>
  <el-container>
    <el-header>
      <div class="header_left">
        <img src="../../assets/logo.png" @click="goHome" alt="">
        <i class="submenuControl" :class="isCollapse?'el-icon-s-unfold':'el-icon-s-fold'" @click="submenuControl"></i>
      </div>
      <div class="header_right">
        <div class="userInfo">
          <el-dropdown>
            <span class="el-dropdown-link">
              {{userInfo.name}}<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="loginOut">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </el-header>
    <el-main>
      <div class="content">
        <div class="aslide">
          <el-menu class="el-menu-vertical-demo" :collapse="isCollapse" :router="true" :unique-opened="true" :default-active="defaultActive" @select="handleMenuSelect">
            <el-submenu :index="item.f_id +''" v-for="item in rightList">
              <template #title>
                <i :class="item.f_icon"></i>
                <span>{{item.f_name}}</span>
              </template>
              <el-menu-item-group v-for="children in item.children">
                <el-menu-item :index="children.f_url">{{children.f_name}}</el-menu-item>
              </el-menu-item-group>
            </el-submenu>
          </el-menu>
        </div>
        <div class="main">
          <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/' }">
              <i class="el-icon-s-home"></i>
              <span> 首页</span>
            </el-breadcrumb-item>
            <el-breadcrumb-item v-for="item in breadcrumbListData" :to="{ path: item.f_url }">{{item.f_name}}</el-breadcrumb-item>
          </el-breadcrumb>
          <router-view></router-view>
        </div>
      </div>
    </el-main>
    <el-footer>
      @2021
    </el-footer>
  </el-container>
</template>

<script lang="ts">
import { defineComponent, ref, computed, onMounted, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useStore } from 'vuex'
import jsCookie from 'js-cookie'

export default defineComponent({
  setup() {
    let isCollapse = ref<boolean>(false)
    const router = useRouter()
    const route = useRoute()
    const store = useStore()
    const defaultActive = ref('')
    let indexBreadcrumbs = ref()
    let breadcrumbListData = ref<any>(
      JSON.parse(sessionStorage.getItem('breadcrumbListData') || '[]') || ''
    )
    const submenuControl = () => {
      isCollapse.value = !isCollapse.value
    }
    const loginOut = () => {
      jsCookie.remove('token')
      router.push({
        path: '/login',
      })
    }
    const goHome = () => {
      router.push({
        path: '/home',
      })
    }
    watch(route, () => {
      defaultActive.value = route.path
      breadcrumbList()
      if (route.path === '/welcome') {
        breadcrumbListData.value = []
        sessionStorage.setItem('breadcrumbListData', '[]')
      }
    })
    const handleMenuSelect = (index, indexPath) => {
      defaultActive.value = index
      indexBreadcrumbs.value = indexPath
    }
    onMounted(() => {
      defaultActive.value = route.path
    })
    const breadcrumbList = () => {
      let menuList = JSON.parse(jsCookie.get('rightList') || '[]')
      let breadList: any[] = []
      if (indexBreadcrumbs.value) {
        indexBreadcrumbs.value.map((item) => {
          if (item === '/welcome') {
            return false
          } else {
            for (let i = 0; i < menuList.length; i++) {
              menuList[i].children.map((childrenItem) => {
                if (item === childrenItem.f_url) {
                  breadList.push(menuList[i])
                  breadList.push(childrenItem)
                }
              })
            }
          }
          breadcrumbListData.value = breadList
          sessionStorage.setItem(
            'breadcrumbListData',
            JSON.stringify(breadcrumbListData.value)
          )
        })
      }
    }
    return {
      isCollapse,
      submenuControl,
      loginOut,
      rightList: computed(() => store.state.rightList),
      userInfo: JSON.parse(jsCookie.get('userInfo') || ''),
      goHome,
      defaultActive,
      handleMenuSelect,
      breadcrumbList,
      breadcrumbListData,
    }
  },
})
</script>


<style scoped lang="scss">
@import '../../assets/common.scss';
.el-container {
  display: flex;
  height: 100vh;
  background: #fcfcfc;
  .el-header {
    display: flex;
    background: $color-7dbcea;
    align-items: center;
    .header_left {
      img {
        display: inline;
        width: 50px;
        height: 50px;
        vertical-align: middle;
        border-radius: 50%;
      }
      .submenuControl {
        display: inline-block;
        font-size: 30px;
        color: #fff;
        margin: 8px 20px;
      }
    }
    .header_right {
      flex: 1;
      .userInfo {
        float: right;
        line-height: 60px;
      }
    }
  }
  .el-main {
    padding: 0;
    .el-breadcrumb {
      padding: 15px 0;
    }
    .content {
      display: flex;
      height: 100%;
      .aslide {
        .el-menu {
          height: 100%;
        }
        .el-menu-vertical-demo:not(.el-menu--collapse) {
          width: 200px;
        }
      }
      .main {
        flex: 1;
        background: #fff;
        margin: 10px;
        border-radius: 20px;
        overflow-x: hidden;
        overflow-y: auto;
        /* 设置滚动条的样式 */
        &::-webkit-scrollbar {
          width: 6px;
        }
        /* 滚动槽 */
        &::-webkit-scrollbar-track {
          -webkit-box-shadow: inset006pxrgba(0, 0, 0, 0.3);
          border-radius: 10px;
        }
        /* 滚动条滑块 */
        &::-webkit-scrollbar-thumb {
          border-radius: 10px;
          background: rgba(0, 0, 0, 0.1);
          -webkit-box-shadow: inset006pxrgba(0, 0, 0, 0.5);
        }
        &::-webkit-scrollbar-thumb:window-inactive {
          background: rgba(255, 0, 0, 0.4);
        }
      }
    }
  }
  .el-footer {
    background: $color-7dbcea;
    line-height: 60px;
    text-align: center;
    color: #888;
  }
}
</style>